package helper;

/**
 * Created with IntelliJ IDEA.
 * User: Eugene
 * Date: 12/23/12
 * Time: 3:34 PM
 * To change this template use File | Settings | File Templates.
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class ExcelWriter {

    private WritableCellFormat timesBold;
    private WritableCellFormat times;
    private String inputFile;

    public void setOutputFile(String inputFile) {
        this.inputFile = inputFile;
    }

    public void write(Double a) throws IOException,
            WriteException {
        File file = new File(inputFile);
        WorkbookSettings wbSettings = new WorkbookSettings();
        wbSettings.setLocale(new Locale("en", "US"));
        WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
        workbook.createSheet("Report", 0);
        WritableSheet excelSheet = workbook.getSheet(0);
        WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
        // Define the cell format
        times = new WritableCellFormat(times10pt);
        // Lets automatically wrap the cells
        times.setWrap(true);
        addDouble(excelSheet, 0, 0, a);
        workbook.write();
        workbook.close();
        System.out.println("Please check the result file under "
                + this.inputFile);
    }

    /*  private void createLabel(WritableSheet sheet)
            throws WriteException {
        // Lets create a times font
        WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
        // Define the cell format
        times = new WritableCellFormat(times10pt);
        // Lets automatically wrap the cells
        times.setWrap(true);

        // Create create a bold font with unterlines
        WritableFont times10ptBold = new WritableFont(
                WritableFont.TIMES, 10, WritableFont.BOLD, false);
        timesBold = new WritableCellFormat(times10ptBold);
        // Lets automatically wrap the cells
        timesBold.setWrap(true);

        CellView cv = new CellView();
        cv.setFormat(times);
        cv.setFormat(timesBold);
        cv.setAutosize(true);

        sheet.setColumnView(1, cv);
        sheet.setColumnView(2, cv);
        sheet.setColumnView(3, cv);
        sheet.setColumnView(6, cv);

        // Write a few headers
        addCaption(sheet, 0, 0, "#");
        addCaption(sheet, 1, 0, "Name");
        addCaption(sheet, 2, 0, "Office");
        addCaption(sheet, 3, 0, "Destination");
        addCaption(sheet, 4, 0, "Start date");
        addCaption(sheet, 5, 0, "End date");
        addCaption(sheet, 6, 0, "Current status");


    }*/

    /*private void createContent(WritableSheet sheet, int i, Trf trf)
            throws WriteException, RowsExceededException {

        addNumber(sheet, 0, i, i);
        addLabel(sheet, 1, i, trf.getEmployee().getFirstName() + " "
                + trf.getEmployee().getLastName());
        addLabel(sheet, 2, i, trf.getEmployee().getOffice().getName()
                + ", " + trf.getEmployee().getOffice().getCity().getName());
        addLabel(sheet, 3, i, trf.getCity().getName() + ", "
                + trf.getCity().getCountry().getName());
        addLabel(sheet, 4, i, trf.getDepartureDate().toString());
        addLabel(sheet, 5, i, trf.getReturnDate().toString());
        addLabel(sheet, 6, i,
                trf.getTrfActionByCurrentStatus().getStatus().getType());

    }
*/
    /*private void addCaption(WritableSheet sheet, int column, int row, String s)
            throws RowsExceededException, WriteException {
        Label label;
        label = new Label(column, row, s, timesBold);
        sheet.addCell(label);
    }
     */
    private void addDouble(WritableSheet sheet, int column, int row,
                           Double d) throws WriteException, RowsExceededException {
        Number number;
        number = new Number(column, row, d, times);
        sheet.addCell(number);
    }

    /* private void addLabel(WritableSheet sheet, int column, int row, String s)
            throws WriteException, RowsExceededException {
        Label label;
        label = new Label(column, row, s, times);
        sheet.addCell(label);
    }*/
}
